<?php

namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\admin\model\Car;

class Article extends Controller {

	public function lst() {

		$list = Db::name('car')->paginate(5);
		$this->assign('list', $list); // 赋值数据集
		$cates = Db::name('cate')->select();
		$this->assign('cates', $cates);
		return $this->fetch();
	}
	public function edit() {



		$artres = Db::name('article')->where('id', input('get.id'))->find();
		$this->assign('artres', $artres); // 赋值数据集
		$cates = Db::name('cate')->select();
		$this->assign('cates', $cates);
		return $this->fetch();
	}
	// 增加车辆
	public function add()
	{
		// var_dump(input('post.'));
		$file = request()->file('pic');
		$a = ROOT_PATH . 'public' . DS . 'uploads';

		$brand = input('post.brand');
		$desc = input('post.desc');
		$site = input('post.site');
		$city = input('post.city');
		$price = input('post.price');
		$info = $file->move($a);
		$article = input('post.article');
		$pic = '/uploads/'.$info->getSaveName();
		$car = new Car;
		$car -> brand = $brand;
		$car -> desc = $desc;
		$car -> site = $site;
		$car -> city = $city;
		$car -> pic = $pic;
		$car -> article = $article;
		$car -> price = $price;
		$result = $car -> save();
		$this->redirect('/admin');
	}
	public function edits($id) {
		$article = D('article');
		if (IS_POST) {
			$data['title'] = I('title');
			$data['id'] = I('id');
			$data['rizu'] = I('rizu');
			$data['num'] = I('num');
			$data['atype'] = I('atype');
			$data['author'] = I('author');
			$data['cateid'] = I('cateid');
			$data['content'] = I('content');
			$data['keywords'] = I('keywords');
			$data['des'] = I('des');
			$data['rem'] = I('rem');
			$data['time'] = time();
			if ($_FILES['pic']['tmp_name'] != '') {
				$upload = new \Think\Upload(); // 实例化上传类
				$upload->maxSize = 3145728; // 设置附件上传大小
				$upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
				$upload->savePath = './Public/Uploads/'; // 设置附件上传目录
				$upload->rootPath = './';
				// 上传文件
				$info = $upload->uploadOne($_FILES['pic']);
				if (!$info) {
// 上传错误提示错误信息
					$this->error($upload->getError());
				} else {
// 上传成功
					$data['pic'] = $info['savepath'] . $info['savename'];
				}
			}

			if ($article->create($data)) {
				if ($article->save()) {
					$this->success('修改文章成功！', U('lst'));
				} else {
					$this->error('修改文章失败！');
				}

			} else {
				$this->error($article->getError());
			}

			return;
		}

		$artres = $article->find($id);
		$this->assign('artres', $artres);

		$cate = D('cate');
		$cates = $cate->catetree();
		$this->assign('cates', $cates);
		$this->display();
	}

	public function del()
	{
		$id = input('get.id');
		$result = Car::destroy($id);
		$this->redirect('/admin');
	}
}
